package algorithm.swordoff;

/**
 * 数值的整数次方
 *
 * 快速幂的O(logn)实现
 * leetcode上的测试用例有点恶心
 * 和主站50题一样
 */

public class SQ16 {
    public double myPow(double x, int n) {
        return n < 0 ? 1/myPowHelper(x, -n) : myPowHelper(x, n);
    }

    public double myPowHelper(double base, int exp) {
        if (exp == 0) return 1.0;
        double y = myPowHelper(base, exp/2);
        return (exp&1) == 1 ? y*y*base : y*y;
    }

    public static void main(String[] args) {
        SQ16 sos = new SQ16();
        System.out.println(sos.myPow(1.0, -2147483648));
    }
}
